<script>

import { useDictTypeApi, useDictTypeSubmitApi } from '@/api/system/dict'

export default {
  name: 'DictEdition',
  data() {
    return {
      visible: false,
      dataForm: {
        id: '',
        dictType: '',
        dictName: '',
        sort: 0,
        dictSource: 0,
        dictSql: '',
        remark: ''
      },
      dataRules: {
        dictType: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        dictName: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        dictSource: [{ required: true, message: '必填项不能为空', trigger: 'blur' }],
        dictSql: [{ required: true, message: '必填项不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    init(id) {
      this.visible = true
      this.dataForm.id = ''
      const formInstance = this.$refs.dataFormRef
      if (formInstance) {
        formInstance.resetFields()
      }
      if (id) {
        this.getBaseClass(id)
      }
    },
    getBaseClass(id) {
      useDictTypeApi(id).then(res => {
        Object.assign(this.dataForm, res.data)
      })
    },
    submitHandle() {
      this.$refs.dataFormRef.validate((valid) => {
        if (!valid) {
          return false
        }
        useDictTypeSubmitApi(this.dataForm).then(() => {
          this.$message.success({
            message: '操作成功',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        })
      })
    }
  }
}
</script>

<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false">
    <el-form ref="dataFormRef" :model="dataForm" :rules="dataRules" label-width="80px">
      <el-form-item prop="dictType" label="字典类型">
        <el-input v-model="dataForm.dictType" placeholder="字典类型" />
      </el-form-item>
      <el-form-item prop="dictName" label="字典名称">
        <el-input v-model="dataForm.dictName" placeholder="字典名称" />
      </el-form-item>
      <el-form-item prop="sort" label="排序">
        <el-input-number v-model="dataForm.sort" controls-position="right" :min="0" label="排序" />
      </el-form-item>
      <el-form-item prop="remark" label="备注">
        <el-input v-model="dataForm.remark" placeholder="备注" />
      </el-form-item>
      <el-form-item label="数据来源" prop="dictSource">
        <el-radio-group v-model="dataForm.dictSource">
          <el-radio-button :label="0">字典数据</el-radio-button>
          <el-radio-button :label="1">动态SQL</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item v-if="dataForm.dictSource === 1" prop="dictSql" label="动态SQL">
        <el-input
          v-model="dataForm.dictSql"
          type="textarea"
          placeholder="如：select id as dictValue, name as dictLabel, 'info' as labelClass from sys_role where deleted = 0"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="submitHandle()">确定</el-button>
    </template>
  </el-dialog>
</template>

<style scoped lang="scss">

</style>
